<template>
  <div class="country">
    <el-row>
      <el-button icon="el-icon-plus" @click="dialogVisibleadd = true"
        >添加</el-button
      >
      <el-button icon="el-icon-delete" @click="dele">删除</el-button>
      <!-- <el-button icon="el-icon-download">导出文档</el-button> -->
    </el-row>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      :row-style="{ height: '48px' }"
      height="calc(100vh - 200px)"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClick(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="Id" label="编号" width="150" />
      <el-table-column prop="Township" label="乡" width="150" />
    </el-table>
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <el-form-item label="乡镇">
          <el-input v-model="inputcode" placeholder="请输入乡镇名称" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="handleClose()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog
      title="添加"
      :visible.sync="dialogVisibleadd"
      width="30%"
      :before-close="handleCloseadd"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <el-form-item label="乡镇">
          <el-input v-model="inputcode" placeholder="请输入乡镇名称" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="handleCloseadd()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { API_AREA_TOWNSHIP } from "../../api/table";
import { API_TOW_ADD, API_TOW_DELE, API_TOW_CHGE } from "../../api/system";
export default {
  name: "",
  data() {
    return {
      msg: "乡管理",
      labelPosition: "right",
      dialogVisible: false, // 修改模块显示隐藏
      dialogVisibleadd: false, // 修改模块显示隐藏
      tableData: [],
      inputcode: "",
      multipleSelection: [],
      id: "",
    };
  },
  methods: {
    handleClick(row) {
      this.inputcode = row.Township;
      this.dialogVisible = true;
      this.id = row.Id;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleClose() {
      this.$confirm("确认修改？")
        .then((_) => {
          // done();
          let data = { township: this.inputcode, Id: this.id };
          API_TOW_CHGE(data)
            .then((res) => {
              console.log(res);
              this.dialogVisible = false;
              this.submit();
              this.clear();
              this.$message.success("修改成功");
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch((_) => {});
    },
    handleCloseadd() {
      this.$confirm("确认添加？")
        .then((_) => {
          // done();
          let data = { township: this.inputcode };
          API_TOW_ADD(data)
            .then((res) => {
              console.log(res);
              this.inputcode = "";
              this.$message.success("添加成功!");
              this.submit();
              this.dialogVisibleadd = false;
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch((_) => {});
    },
    clear() {
      this.inputcode = "";
      this.id = "";
      this.dialogVisible = false; // 修改模块显示隐藏
      this.dialogVisibleadd = false; // 修改模块显示隐藏
    },
    submit() {
      API_AREA_TOWNSHIP()
        .then((res) => {
          console.log(res);
          this.tableData = res.ResList;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    dele() {
      let data = this.multipleSelection.map((item) => {
        return {
          Id: item.Id,
          Township: item.Township,
        };
      });
      if (data.length != 0) {
        API_TOW_DELE(data)
          .then((res) => {
            console.log(res);
            this.submit();
            this.$message.success("删除成功！");
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
  },
  created() {
    this.submit();
  },
};
</script>

<style scoped lang="scss">
.country {
  box-sizing: border-box;
  padding: 30px;
  // max-width: 570px;
  .el-row {
    text-align: left;
  }

  .el-table {
    margin-top: 20px;
  }
}
</style>
